package com.lsh.controller;

import com.lsh.service.CommonService;
import com.lsh.service.RegisterService;
import com.lsh.util.result.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * 公共类模块
 */
@Controller
@RequestMapping("/common")
public class CommonController {

    @Autowired
    CommonService commonService;
    @Autowired
    RegisterService registerService;
    /**
     * 获取天气预报
     * @param province
     * @param city
     * @return
     */
    @RequestMapping(value = "/weather",method = RequestMethod.GET)
    public ModelAndView weatherForecast(@RequestParam("province") String province, @RequestParam("city") String city, ModelAndView mv){
        if(province == null || city == null){
            return null;
        }else{
            try {
                province = URLEncoder.encode(URLDecoder.decode(province, "UTF-8"), "UTF-8");
                city = URLEncoder.encode(URLDecoder.decode(city, "UTF-8"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        }
        String result = commonService.weatherForecast(province, city);
        mv.addObject("data", result);
        mv.setViewName("/JSON");
        return mv;
    }

    /**
     *
     * @param authCode
     * @return
     */
    @GetMapping("/activateregister")
    public ModelAndView activateAccount(@RequestParam("code") String authCode) {
        boolean bool = registerService.createUser(authCode);
        ModelAndView mv = new ModelAndView();
        if(bool){
            registerService.removeUser(authCode);
            mv.addObject("message", "账号激活成功");
        }
        else{
            mv.addObject("message", "账号激活失败,请重新注册");
        }
        mv.setViewName("activateregister");
        return mv;
    }

}
